package com.demo.boot.desigin.factory;

import com.demo.boot.desigin.factory.enums.ShapeType;

import javax.lang.model.type.UnknownTypeException;

/**
* @author ccubee
* @since 2021-05-12 11:15
*/
public class ShapeFactory {

    public static Object getClass(Class<? extends Shape> clazz) {
        Object obj = null;

        try {
            obj = Class.forName(clazz.getName()).newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return obj;
    }

    public static <T> T getClass1(Class<? extends T> clazz) {
        T obj = null;

        try {
            obj = (T) Class.forName(clazz.getName()).newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return obj;
    }

    public static  <T> T getClass2(Class<? extends T> clazz) {
        T obj = null;
        try {
            obj = clazz.getDeclaredConstructor().newInstance();
        }
        catch (ReflectiveOperationException e) {
            e.printStackTrace();
        }
        return obj;
    }


    public static Shape getShape(ShapeType type){
        switch(type){
            case CIRCLE:
                return new Circle();
            case RECTANGLE:
                return new Rectangle();
            case SQUARE:
                return new Square();
            default:
//                throw new UnknownTypeException();
                return new Circle();
        }
    }
}